package controller;

//ע����ĵط�����Ϊ����servlet���õ�
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class SendPack {
  //����get����    url?a=x&b=xx��ʽ
  public static String sendGet(String url,String param){
      String result = "";
      BufferedReader in = null;
      try{
          String urlName = "";
          param = param.replace(",","&");
          param = param.replace("\"","");
          param = param.replace("\'","");
          if(param.length() != 0){
              urlName = url + "?" +param;
          }
          else urlName = url;
          URL resUrl = new URL(urlName);
          URLConnection urlConnec = resUrl.openConnection();
          urlConnec.setRequestProperty("accept", "*/*");
          urlConnec.setRequestProperty("connection", "Keep-Alive");
          urlConnec.setRequestProperty("Charset", "utf-8");
          urlConnec.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
          urlConnec.connect();
          Map<String, List<String>> map = urlConnec.getHeaderFields();
          for (String key : map.keySet()) {
              System.out.println(key + "--->" + map.get(key));
          }
           // ���� BufferedReader����������ȡURL����Ӧ
          in = new BufferedReader(
        		  new InputStreamReader(urlConnec.getInputStream(),
        				  "utf-8"));
          String line;
          while ((line = in.readLine()) != null) {
              result += line;
          }
      }catch(Exception e){
          System.out.println("����get����ʧ��"+e);
          e.printStackTrace();
      }finally{
          try{
              if(in != null){
                  in.close();
              }
          }catch(Exception e){
              e.printStackTrace();
          }
      }
      return result;
  }
  //����post����
  public static String sendPost(String url,String param){
      String result = "";
      PrintWriter out = null;
      BufferedReader in = null;
      param = param.replace(",","&");
      param = param.replace("\"","");
      param = param.replace("\'","");
      try{
          URL resUrl = new URL(url);
          URLConnection urlConnec = resUrl.openConnection();
          urlConnec.setRequestProperty("accept", "*/*");
          urlConnec.setRequestProperty("connection", "Keep-Alive");
          urlConnec.setRequestProperty("Charset", "utf-8");
          urlConnec.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
          // ����POST�������������������
          urlConnec.setDoInput(true);
          urlConnec.setDoOutput(true);
          
          out = new PrintWriter(urlConnec.getOutputStream());
          out.print(param.getBytes("UTF-8"));//����post����
          out.flush();
           // ���� BufferedReader����������ȡURL����Ӧ
          in = new BufferedReader(new InputStreamReader(urlConnec.getInputStream()));
          String line;
          while ((line = in.readLine()) != null) {
              result += line;
          }
      }catch(Exception e){
          System.out.println("post������ʧ��"+e);
          e.printStackTrace();
      }finally{
          try{
              if(in != null){
                  in.close();
              }
              if(out != null){
                  out.close();
              }
          }catch(Exception e){
              e.printStackTrace();
          }
      }
      return result;
  }
  /**����
   * ˵�������������˹�Ʊ�ӿ���get����,���˹�Ʊ�ӿڲ�֧��jsonp,����post,��Ϊ�����õĹ�˾�ĽӿھͲ�չʾ��,һ���,һ��url,һ����ݰ�
   */
  /*
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      String resultGet = sendGet("http://hq.sinajs.cn/list=sh600389","");
      System.out.println(resultGet);
  }
  */
  
}